package model;

import java.security.InvalidParameterException;
import java.util.ArrayList;

/**
 * 
 * @author Mads Nielsen
 */
public class Item {

    private String name;
    private String description;
    private ArrayList<Item> subItems;
    private String unit;
    private int qty;
    private String itemNo;
    private String location;
    private double price;
    private double discount;
    private double vendorPrice;
    private int stock;
    private int minStock;
    private int maxStock;

    public Item() {
        subItems = new ArrayList<Item>();
    }

    /**
     * Adds supplied item to the set of subitems
     * 
     * @param item
     *            ItemDescription to add to the set of subitems
     */
    public void addSubItem(Item item) {
        if (item == this) {
            throw new InvalidParameterException(
                    "Item cannot be a subitem of itself");
        }

        // TODO prevent circular references (i.e. A points to B and B points to
        // A)

        subItems.add(item);
    }

    /**
     * Fetch non-reference array of subitems
     * 
     * @return Array of items contained in this item
     */
    public Item[] getSubItems() {
        return subItems.toArray(new Item[subItems.size()]);
    }

    /**
     * Checks if the item contains one or more subitems
     * 
     * @return true if object contains one or more items, false otherwise
     */
    public boolean hasSubItems() {
        return getSubItems().length > 0;
    }

    /**
     * Calculates total price of item and all subitems, discounts included
     * 
     * @return
     */
    public double getTotalPrice() {

        double total = 0;

        // check for subitems
        if (hasSubItems()) {
            for (Item item : getSubItems()) {
                total += item.getTotalPrice();
            }
        }
        
        total += getPrice();
        total -= (getDiscount() / 100) * total;

        return total;
    }

    /* Trivial getter and setters */
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getUnit() {
        return unit;
    }

    public void setUnit(String unit) {
        this.unit = unit;
    }

    public int getQty() {
        return qty;
    }

    public void setQty(int qty) {
        this.qty = qty;
    }

    public String getItemNo() {
        return itemNo;
    }

    public void setItemNo(String itemNo) {

        this.itemNo = itemNo;

    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public double getDiscount() {
        return discount;
    }

    public void setDiscount(double discount) {
        this.discount = discount;
    }

    public double getVendorPrice() {
        return vendorPrice;
    }

    public void setVendorPrice(double vendorPrice) {
        this.vendorPrice = vendorPrice;
    }

    public int getStock() {
        return stock;
    }

    public void setStock(int stock) {
        this.stock = stock;
    }

    public int getMinStock() {
        return minStock;
    }

    public void setMinStock(int minStock) {
        this.minStock = minStock;
    }

    public int getMaxStock() {
        return maxStock;
    }

    public void setMaxStock(int maxStock) {
        this.maxStock = maxStock;
    }

    public void removeFromSubItems(Item subItem) {
        subItems.remove(subItem);
    }
}
